package com.kcl.dfss;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

public class TestActivity extends Activity {

	private Button mStartService = null;
	private Button mGetDistance = null;
	private static TextView mStatusView = null;
	private static ListView mListView = null;
	private boolean StartWork = false;
	private static boolean CREATED = false;
	private static List<String> mDataList = null;
	private static DataAdapter mAdapter = null;
	private static Handler mHandler = new Handler() {
		public void handleMessage(Message msg) {
			if (msg.what == 2) {
				Log.d("TestActivity", "update listview");
				mAdapter.addItem((String) msg.obj);
				mListView.invalidate();
			} else {
				String msgString = (String) msg.obj;
				mStatusView.setText(msgString);
			}
		}
	};

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_test);
		mStatusView = (TextView) findViewById(R.id.serviceStatus);
		mStartService = (Button) findViewById(R.id.startService);
		mGetDistance = (Button) findViewById(R.id.getDistance);

		if (LocationService.getInstance() == null) {
			Log.d("TestActivity", "start location Service");
			Intent intent = new Intent(getApplicationContext(),
					LocationService.class);
			intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			startService(intent);
		}

		mStartService.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (!StartWork) {
					LocationService.getInstance().startCaculateDistance();

				} else {
					LocationService.getInstance().stopCaculateDistance();
				}
				StartWork = !StartWork;
			}
		});
		mGetDistance.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				String distance = String.valueOf(LocationService.getDistance());
				String msg = null;
				Location location = LocationService.getLatestLocation();
				if (location != null) {
					msg = distance + " "
							+ String.valueOf(location.getLatitude()) + " "
							+ String.valueOf(location.getLongitude());
				} else {
					msg = distance;
				}
				// addDistanceToView(LocationService.getDistance());
				Message message = new Message();
				message.what = 2;
				message.obj = "onclick";
				mHandler.sendMessage(message);
			}
		});
		mDataList = new ArrayList<String>(); 
		mDataList.add("this is the list of data");
		mListView = (ListView) findViewById(R.id.listview);
		mAdapter = new DataAdapter(this, R.layout.layout_listitem,
				R.id.listitem, mDataList);
		mListView.setAdapter(mAdapter);
		CREATED = true;
	}

	public static void addLocationDataToView(double longtitude,
			double latitude, double speed, float accuracy) {
		if (!CREATED)
			return;
		Log.d("TextActivity",
				"add location " + "Long: " + String.valueOf(longtitude)
						+ " Latit:" + String.valueOf(latitude) + " Speed: "
						+ String.valueOf(speed)+" Accuracy: "+accuracy);
		// mAdapter
		// .addItem(
		String msg = "Long: " + String.valueOf(longtitude) + " Latit:"
				+ String.valueOf(latitude) + " Speed: " + String.valueOf(speed);
//		Message message = new Message();
//		message.what = 2;
//		message.obj = msg;
//		mHandler.sendMessage(message);
	}

	public static void addDistanceToView(float distance) {
		Log.d("TestActivity", "add distance " + distance);
		String msg = "";
		if (distance < 1000.0f) {
			msg = "Distance: " + distance + " m";
		} else {
			msg = "Distance: " + (distance / 1000.0f) + " km";
		}
		// mListView.invalidate();
//		Message message = new Message();
//		message.what = 2;
//		message.obj = msg;
//		mHandler.sendMessage(message);
//		mHandler.sendEmptyMessage(2);
	}

	public void onDestroy() {
		super.onDestroy();
		CREATED = false;
	}

	public class DataAdapter extends BaseAdapter {
		private ArrayList<String> mData = null;
		private Context mContext = null;
		private int mLayoutID = -1;
		private int mViewID = -1;
		private LayoutInflater mInflater = null;

		public DataAdapter(Context context, int resourceid, int textviewid,
				List<String> data) {
			mContext = context;
			mLayoutID = resourceid;
			mViewID = textviewid;
			mData = new ArrayList<String>(data);
			mInflater = (LayoutInflater) mContext
					.getSystemService(LAYOUT_INFLATER_SERVICE);
		}

		public void addItem(String string) {
			mData.add(string);
			notifyDataSetChanged();
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return mData.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return mData.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			View view = mInflater.inflate(mLayoutID, null);
			TextView textview = (TextView) view.findViewById(mViewID);
			textview.setText(mData.get(position));
			return view;
		}

	}
}
